<?php

namespace App\Http\Controllers\Wechat;

use App\Http\Controllers\Controller;

use App\Models\Wechat\Mpnews;
use App\Models\Wechat\Text;
use App\Models\Wechat\Image;

use EasyWeChat\Kernel\Messages\News;
use EasyWeChat\Kernel\Messages\NewsItem;
use EasyWeChat\Kernel\Messages\Image as WechatImage; 
use EasyWeChat\Kernel\Messages\Text as WechatText;
use Log;
use DB;

class ReplyController 
{
    var $app;

    var $wechat_image_id;

    var $wechat_mpnews_id;

    var $wechat_text_id;

    public function __construct($app,$wechat_image_id,$wechat_mpnews_id,$wechat_text_id){
        $this->app = $app;
        $this->wechat_image_id = $wechat_image_id;
        $this->wechat_mpnews_id = $wechat_mpnews_id;
        $this->wechat_text_id = $wechat_text_id;
    }

    public function entrance(){
        if($this->wechat_image_id){
            return $this->image();
        }else if($this->wechat_mpnews_id){
            return $this->mpnew();
        }else if($this->wechat_text_id){
            return $this->text();
        }
    }

    /**
     * 图文回复
     * @param  [type] $wechat_mpnews_id [description]
     * @return [type]                   [description]
     */
    public function mpnew(){
    	$mpnews = new Mpnews();
    	$mpnews = $mpnews->cacheFind($this->wechat_mpnews_id);
		if(!$mpnews){
            Log::error('调用了不存在的wechat_mpnews_id'.$this->wechat_mpnews_id);
			return '';
		}
		
		foreach ($mpnews as $mpnew) {
			$items[] = new NewsItem([
                'title'       => $mpnew->title,
                'description' => $mpnew->desc,
                'url'         => $mpnew->link,
                'image'       => $mpnew->pic,
			]);
		}

		return new News($items);
    }

    /**
     * 图片回复
     * @param  [type] $wecaht_image_id [description]
     * @return [type]                  [description]
     */
    public function image(){
    	$image = new Image();
    	$image = $image->cacheFind($this->wechat_image_id);
    	if(!$image){
            Log::error('调用了不存在的wechat_image_id'.$this->wechat_image_id);
    		return '';
    	}
        //未上传、已过期
        if(!$image->mediaId || strtotime($image->mediaTime)<time()){
            $result = $this->app->media->uploadImage(base_path().'/public'.$image->url);
            if(!$result || !isset($result['media_id'])){
                Log::error('回复图片:'.$this->wechat_image_id.';上传微信临时素材失败');
                return '';
            }
            DB::table('wechat_image')->where('id',$this->wechat_image_id)->update([
                'mediaId' => $result['media_id'],
                'mediaTime' => date('Y-m-d H:i:s',$result['created_at']+86400*3),
            ]);
            $image->cacheEmpty($this->wechat_image_id);
            $image->mediaId = $result['media_id'];
        }
        return new WechatImage($image->mediaId);
    }

    /**
     * 文本回复
     * @param  [type] $wechat_text_id [description]
     * @return [type]                 [description]
     */
    public function text(){
        $text = new Text();
        $text = $text->cacheFind($this->wechat_text_id);
        if(!$text){
            Log::error('调用了不存在的wechat_text_id'.$this->wechat_text_id);
            return '';
        }
        return new WechatText($text->content);
    }
}
